package com.puwang.tbmpro;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/**
 * Created by liutao on 2016/10/22.
 */
public class CameraSerfaceView extends SurfaceView {

    private Camera camera = null;
    private SurfaceHolder holder = null;

    public CameraSerfaceView(Context context) {
        super(context);
        holder = this.getHolder();

        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
                Camera.Parameters parameters = camera.getParameters();
                //以下注释掉的是设置预览时的图像以及拍照的一些参数
                // parameters.setPictureFormat(PixelFormat.JPEG);
                // parameters.setPreviewSize(parameters.getPictureSize().width,
                // parameters.getPictureSize().height);
                // parameters.setFocusMode("auto");
                // parameters.setPictureSize(width, height);
                camera.setParameters(parameters);
                camera.startPreview();
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                camera = Camera.open();

                try {
                    //设置camera预览的角度，因为默认图片是倾斜90度的
                    camera.setDisplayOrientation(90);
                    //设置holder主要是用于surfaceView的图片的实时预览，以及获取图片等功能，可以理解为控制camera的操作..
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    camera.release();
                    camera = null;
                    e.printStackTrace();
                }

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                //顾名思义可以看懂
                camera.stopPreview();
                camera.release();
                camera = null;
            }
        });
//          holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
}
